<template>
  <el-dialog
    :title="textMap[dialogStatus]"
    :visible.sync="dialogFormVisible2"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :fullscreen="false"
  >
    <el-form
      ref="dataForm"
      :rules="rules"
      :model="formData"
      label-position="left"
      label-width="120px"
      style="width: 500px; margin-left:50px;"
    >
      <el-form-item label="代码" prop="code">
        <el-input v-model="formData.code" />
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="formData.name" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible2 = false">
        取消
      </el-button>
      <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
        确认
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import * as apiAppDic from '@/api/app_dic'
import * as apiApp from '@/api/app'

export default {
  name: 'CreateAppDic',
  props: ['textMap', 'dialogStatus', 'dialogFormVisible'],
  computed: {
    dialogFormVisible2: {
      get() {
        return this.dialogFormVisible
      },
      set(val) {
        // dialogFormVisible改变由父组件控制
        this.$emit('update:dialogFormVisible', val)
      }
    }
  },
  created() {
  },
  data() {
    return {
      formData: {
        id: undefined,
        appId: undefined,
        name: undefined,
        code: undefined,
      },
      rules: {
        name: [{ required: true, message: '名称必填', trigger: 'blur' }],
        code: [{ required: true, message: '代码必填', trigger: 'blur' }]
      }
    }
  },
  methods: {
    clearFormData() {//清除 this.formData数据
      Object.keys(this.formData).forEach(key => {
        this.formData[key] = undefined
      })
    },
    setAppId(appId) {
      this.formData.appId = appId
    },
    setCurrentRow(currentRowData) {// 修改 某条记录时 获取记录信息
      this.formData.id = currentRowData.id
      this.formData.appId = currentRowData.appId
      this.formData.name = currentRowData.name
      this.formData.code = currentRowData.code
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (this.formData.appId == undefined) {
            alert('必须先选择一个app')
            return
          }
          apiAppDic.createOrUpdate(this.formData).then(() => {
            // 通知父组件刷新列表数据
            this.$emit('on-fresh-list')
            this.dialogFormVisible2 = false
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.formData)
          apiAppDic.createOrUpdate(tempData).then(() => {
            // 通知父组件刷新列表数据
            this.$emit('on-fresh-list')
            this.dialogFormVisible2 = false
            this.$notify({
              title: '成功',
              message: '修改成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
